﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace TiTi.tea.Comm
{
    public class Tools
    {
        /// <summary>
        /// Md5加密，返回32位的字符串
        /// </summary>
        /// <param name="input"></param>
        /// <param name="type">返回值大小写，默认x2，小写，大写是X2</param>
        /// <returns></returns>
        public static string Md5(string input, string type = "x2")
        {
            MD5 md5 = MD5.Create();

            byte[] bytes = Encoding.Default.GetBytes(input);

            byte[] bytesMd5 = md5.ComputeHash(bytes);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytesMd5.Length; i++)
            {
                sb.Append(bytesMd5[i].ToString(type));
            }

            return sb.ToString();
        }


        /// <summary>
        /// 生成随机字符
        /// </summary>
        /// <param name="iLength">生成字符串的长度</param>
        /// <returns>返回随机字符串</returns>
        public static string GetRandCode(int iLength = 16)
        {
            string sCode = "";
            if (iLength == 0)
            {
                iLength = 4;
            }
            string codeSerial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
            string[] arr = codeSerial.Split(',');
            int randValue = -1;
            Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < iLength; i++)
            {
                randValue = rand.Next(0, arr.Length - 1);
                sCode += arr[randValue];
            }
            return sCode;
        }
        public static string GetRandCardCode(string BankName, int iLength = 16)
        {
            string sCode = "";
            switch (BankName)
            {
                case "工商":
                    sCode = "955880";
                    break;
                case "交通":
                    sCode = "622260";
                    break;
                case "农业":
                    sCode = "552599 ";
                    break;
                case "邮政":
                    sCode = "622188";
                    break;
                case "招商":
                    sCode = "955550";
                    break;
                default:
                    sCode = "000000";
                    break;
            }
            if (iLength == 0)
            {
                iLength = 10;
            }
            else
            {
                iLength = iLength - 6;
            }
            string codeSerial = "0,1,2,3,4,5,6,7,8,9";

            string[] arr = codeSerial.Split(',');
            int randValue = -1;
            Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < iLength; i++)
            {
                randValue = rand.Next(0, arr.Length - 1);
                sCode += arr[randValue];
            }
            return sCode;
        }

        private static IdGeneratorOptions options = new IdGeneratorOptions()
        {
            Method = 1,// 1-漂移算法，2-传统算法
            WorkerId = 1,
        };
        /// <summary>
        /// 获取漂移的雪花Id
        /// </summary>
        /// <returns></returns>
        public static Int64 GetSnowFlakeId()
        {
            YitIdHelper.SetIdGenerator(options);
            long newId = YitIdHelper.NextId();

            return newId;
        }

    }
}
